﻿function LogOnModel() {
    this.Username = ko.observable("");
    this.Password = ko.observable("");
    this.RememberMe = ko.observable(false);
    this.WasJustActivated = ko.observable(false);
    this.ActivationKey = ko.observable(null);
}

function RegisterModel() {
    this.Email = ko.observable("").extend({
        required: true,
        maxLength: 50,
        email: true
    });
    this.Username = ko.observable("").extend({
        required: true,
        maxLength: 20
    });
    this.Password = ko.observable("").extend({
        required: true,
        minLength: 8
    });
    this.ConfirmPassword = ko.observable("").extend({
        required: true,
        minLength: 8,
        equal: this.Password
    });
    this.ActivationKey = ko.observable(null);
    this.recaptcha_challenge_field = ko.observable("");
    this.recaptcha_response_field = ko.observable("");
    this.errors = ko.validation.group(this);
    return;
}

function LogOnViewModel(parent) {
    var self = this;
    self.logOnModel = ko.observable();
    self.selectLogOnModel = function() {
        self.logOnModel(new LogOnModel({}));
        self.signInMessageVisible(false);
    };
    self.registerModel = ko.observable();
    self.selectRegisterModel = function() {
        self.registerModel(new RegisterModel({}));
        self.captchaErrorMessageVisible(false);
        self.registerErrorMessageVisible(false);
    };
    self.signInMessage = ko.observable("");
    self.signInMessageVisible = ko.observable(false);

    self.captchaVisible = ko.observable(false);
    self.checkCaptchaVisible = ko.computed(function () {
        if (self.registerModel()) {
            if (self.registerModel().errors().length == 0) {
                self.captchaVisible(true);
            }
        }
    });
   
    self.signIn = function () {
        self.signInMessageVisible(false);
        $.ajax({
            url: "/api/Account/SignIn",
            type: 'post',
            data: self.logOnModel(),
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            },
            statusCode: {
                200: function () {
                    //if it's automatic sign in don't close signin dialog and odnt reload page
                    if (!self.logOnModel().WasJustActivated()) {
                    $("#SignInDialog").modal("hide");
                     window.location.reload();
                    }
                },
                417: function () {
                    self.signInMessage(result.message);
                    self.signInMessageVisible(true);
                 }
            }
        });
    };
    self.captchaErrorMessageVisible = ko.observable(false);
    self.captchaErrorMessage = ko.observable("");
    self.registerErrorMessageVisible = ko.observable(false);
    self.registerErrorMessage = ko.observable("");
    self.username = "";
    self.password = "";
    self.register = function () {
        self.username = self.registerModel().Username();
        self.password = self.registerModel().Password();
        //if (self.registerModel().errors().length == 0) {
        //    self.captchaErrorMessageVisible(false);
        //    self.registerErrorMessageVisible(false);
        //    self.registerModel().recaptcha_challenge_field($("#recaptcha_challenge_field").val());
        //    self.registerModel().recaptcha_response_field($("#recaptcha_response_field").val());
            $.ajax({
                url: "/api/Account/Register",
                type: 'POST',
                data: self.registerModel(),
                statusCode: {
                    200: function () {
                        self.sendEmail();
                        $("#RegisterDialog").modal("hide");
                        location.hash = "#/Confirmation";
                    },
                    417: function (response) {
                        if (response.message == "Please type again, the two words bellow.") {
                            self.captchaErrorMessageVisible(true);
                            self.captchaErrorMessage(result.message);
                        }
                        else {
                            self.registerErrorMessageVisible(true);
                            self.registerErrorMessage(result.message);
                        }
                    }
                }
            });
        //}
    };
    self.sendEmail = function () {
        $.ajax({
            url: "/api/Account/SendWelcomeMessage",
            type: 'post',
            data: self.registerModel()
        });
    };

    self.createProfilesVisible = ko.observable(false);
    self.activateUser = function (activationKey) {
        self.selectRegisterModel();
        self.registerModel().ActivationKey(activationKey);
        $.ajax({
            url: "/api/Account/Activate",
            type: 'POST',
            data: self.registerModel(),
            statusCode: {
                200: function () {
                    self.selectLogOnModel();
                    self.logOnModel().WasJustActivated(true);
                    self.logOnModel().ActivationKey(activationKey);
                    self.signIn();
                    self.createProfilesVisible(true);
                    location.hash = "#/Profiles";
                    window.location.reload();
                }
            }
        });
    };

    self.signOut = function () {
        $.ajax({
            url: "/api/Account/SignOut",
            type: 'post',
            statusCode: {
                200: function () {
                    location.hash = "#/";
                    window.location.reload();
                }
            }
        });
    };
    self.cancelSignIn = function() {
        $("#SignInDialog").modal("hide");
    };
    self.cancelRegister = function() {
        $("#RegisterDialog").modal("hide");
    };

}